package com.zzyl.service.impl;

import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public int addBed(BedDto bedDto) {
        if (bedDto == null) {
            throw new BaseException(BasicEnum.BED_update_FAIL);
        }
        //创建床位对象
        Bed bed=new Bed();
        //拷贝信息
        BeanUtils.copyProperties(bedDto,bed);
        //完善信息
        LocalDateTime now = LocalDateTime.now();
        bed.setBedStatus(0);
        bed.setCreateTime(now);
        bed.setUpdateTime(now);
        //TODO:完善创建人信息
        bed.setCreateBy(1L);
        bed.setUpdateBy(1L);
        //调用添加方法

        return bedMapper.insertBed(bed);
    }

    @Override
    public int deleteById(Integer id) {
        Bed bed = bedMapper.getByBedId(Long.valueOf(id));
        if (bed.getBedStatus()!=0){
            throw new BaseException(BasicEnum.BED_del_FAIL);
        }
        return bedMapper.deleteById(id);
    }

    @Override
    public BedVo getById(Long bedId) {
        BedVo bedVo=new BedVo();
        Bed bed = bedMapper.getByBedId(bedId);
        BeanUtils.copyProperties(bed,bedVo);
        return bedVo;
    }

    @Override
    public int updateById(BedDto dto) {
        //封装床位信息
        Bed bed=new Bed();
        BeanUtils.copyProperties(dto,bed);
        //完善信息
        bed.setUpdateTime(LocalDateTime.now());
        //TODO:完善修改人
        bed.setUpdateBy(1L);
        return bedMapper.updateById(bed);
    }

}

